package info.ask4research.web;

import info.ask4research.model.UnitOfLearning;
import info.ask4research.model.User;
import info.ask4research.service.IUnitOfLearningService;
import info.ask4research.service.impl.UserService;
import info.ask4research.web.dto.SessionDTO;
import info.ask4research.web.dto.UnitOfLearningDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UnitOfLearningController {

	private IUnitOfLearningService unitOfLearningService;
	private UserService userService;

	@RequestMapping(value="jsp/uol/createUoL.action")
	public @ResponseBody Map register(@RequestParam(value="cmd") String cmd,
			@RequestParam(value="title") String title,
			@RequestParam(value="subject") String subject,
			@RequestParam(value="description") String description,
			HttpSession session) throws Exception {

		User user = userService.get((Integer)session.getAttribute("loginId"));
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		if(user != null){
			UnitOfLearning unitOfLearning = new UnitOfLearning();
			unitOfLearning.setTitle(title);
			unitOfLearning.setSubject(subject);
			unitOfLearning.setAuthor(user);
			unitOfLearning.setDescription(description);
			unitOfLearning = unitOfLearningService.create(unitOfLearning);
			session.setAttribute("unitOfLearningId", unitOfLearning.getId());
			modelMap.put("total", 1);
			modelMap.put("unitOfLearningId", unitOfLearning.getId());
			modelMap.put("userId", user.getId());
			modelMap.put("createNewUoL", true);
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("success", false);
		}

		return modelMap;
	}
	
	
	@RequestMapping(value="jsp/configure/session.action")
	public @ResponseBody JSON configureSession(
			@RequestParam(value="id",required=false) String id,
			HttpSession session) throws Exception {
		UnitOfLearning unitOfLearning = new UnitOfLearning();
		User user = userService.get((Integer)session.getAttribute("loginId"));
		if(!id.equals("") || "null".equals(id)){
			unitOfLearning = unitOfLearningService.get(Integer.parseInt(id));	
			session.setAttribute("unitOfLearningId", unitOfLearning.getId());
		} else{
			unitOfLearning = unitOfLearningService.get((Integer)session.getAttribute("unitOfLearningId"));
			}
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		String graphHtml = "";
		if(unitOfLearning.getGraph()!=null &&unitOfLearning.getGraph().getGraphHtml()!=null){
			graphHtml = unitOfLearning.getGraph().getGraphHtml();
		}
		SessionDTO sessionDTO = new SessionDTO(user.getId(), unitOfLearning.getTitle(), unitOfLearning.getId(),graphHtml, "success");
		JsonConfig jsonConfig = new JsonConfig();
		JSON jsonObject = JSONSerializer.toJSON(sessionDTO, jsonConfig);
		
			modelMap.put("userId", user.getId());
			modelMap.put("uolTitle", unitOfLearning.getTitle());
			modelMap.put("uolId", unitOfLearning.getId());
			modelMap.put("success", true);
		

		return jsonObject;
	}
	
	
	@RequestMapping(value="jsp/uol/view.action")
	public @ResponseBody Map uolView(
			HttpSession session,
			@RequestParam(value="limit",required=false) Integer limit,
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="start",required=false) Integer start) throws Exception {

		User user = userService.get((Integer)session.getAttribute("loginId"));
		List<UnitOfLearning> unitOfLearningList = unitOfLearningService.getUnitOfLearningPaginated(limit,page,start);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		int unitOfLearningListSize = unitOfLearningService.getUnitOfLearningList().size();
		
			modelMap.put("userId", user.getId());
			modelMap.put("items", unitOfLearningList);
			modelMap.put("total", unitOfLearningListSize);
			modelMap.put("success", true);
		

		return modelMap;
	}
	
	
	@RequestMapping(value="jsp/uol/viewAutocomplete.action")
	public @ResponseBody Map uolView(
			HttpSession session,
			@RequestParam(value="limit",required=false) Integer limit,
			@RequestParam(value="query",required=false) String query,
			@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="start",required=false) Integer start) throws Exception {

		User user = userService.get((Integer)session.getAttribute("loginId"));
		List<UnitOfLearningDTO> unitOfLearningList = unitOfLearningService.getUnitOfLearningPaginated(limit,page,start,query);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		
			modelMap.put("userId", user.getId());
			modelMap.put("items", unitOfLearningList);
			modelMap.put("total", unitOfLearningList.size());
			modelMap.put("success", true);

		return modelMap;
	}

	public IUnitOfLearningService getUnitOfLearningService() {
		return unitOfLearningService;
	}
	@Resource(name="unitOfLearningService")
	public void setUnitOfLearningService(IUnitOfLearningService unitOfLearningService) {
		this.unitOfLearningService = unitOfLearningService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
